﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp.拦截器
{
    public static class BusinessChainBuilderExtension
    {
        /// <summary>
        /// 添加插件
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="filter"></param>
        public static BusinessChainBuilder<TBusinessChain, TPluginContext> UsePlugin<TBusinessChain, TPluginContext>(this BusinessChainBuilder<TBusinessChain, TPluginContext> builder, IBusinessPlugin<TPluginContext> plugin)
            where TBusinessChain : AbstractBusinessChain<TPluginContext>
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));
            if (plugin == null) throw new ArgumentNullException(nameof(plugin));

            builder.AddPlugin(plugin.Execute);
            return builder;
        }

        /// <summary>
        /// 添加插件
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="filter"></param>
        public static BusinessChainBuilder<TBusinessChain, TPluginContext> UsePlugin<TBusinessChain, TPluginContext>(this BusinessChainBuilder<TBusinessChain, TPluginContext> builder, IEnumerable<IBusinessPlugin<TPluginContext>> plugins)
            where TBusinessChain : AbstractBusinessChain<TPluginContext>
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));
            if (plugins == null) return builder;

            foreach (var plugin in plugins)
            {
                builder.AddPlugin(plugin.Execute);
            }
            return builder;
        }
    }
}
